package responsibility.chain;

import responsibility.filter.Filter;
import responsibility.filter.HttpRequestAndResponesFilter;
import responsibility.http.Request;
import responsibility.http.Respones;

import java.util.ArrayList;
import java.util.List;

public class FilterChain {

    List<HttpRequestAndResponesFilter> chain = new ArrayList();


    int index = 0;

    public void doFilter(Request request, Respones response) {
        if (index == chain.size()) {
            return;
        }
        HttpRequestAndResponesFilter httpRequestAndResponesFilter = chain.get(index);
        index++;
        httpRequestAndResponesFilter.doFilter(request, response);

    }


    public FilterChain addFilter(HttpRequestAndResponesFilter filter) {
        filter.setFilterChain(this);
        this.chain.add(filter);
        return this;
    }


    public FilterChain removeFilter(Filter filter) {
        this.chain.remove(filter);
        return this;
    }

    public List<HttpRequestAndResponesFilter> getChain() {
        return chain;
    }

    public void setChain(List<HttpRequestAndResponesFilter> chain) {
        this.chain = chain;
    }
}
